package com.chixing.day15_map;

import java.util.Comparator;
import java.util.Objects;
import java.util.TreeMap;
import java.util.function.Function;
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;

public class TreeMapDemo {
    public static void main(String[] args) {
        //根据key 排序
        TreeMap<String, Integer> map = new TreeMap<>();
        map.put("电子",3000);
        map.put("服饰",4000);
        map.put("食品",2000);
        map.put("家电",1500);
        System.out.println(map);

        //<商品，销量>

        TreeMap<Product,Integer> productMap = new TreeMap<>();
        Product pro1 = new Product(1,"car",500f);
        Product pro2 = new Product(2,"cat",510f);
        Product pro3 = new Product(3,"gay",520f);

        productMap.put(pro1,11);
        productMap.put(pro2,12);
        productMap.put(pro3,22);
        System.out.println(productMap);

        TreeMap<Product,Integer> treeMap2 = new TreeMap<>(new Comparator<Product>() {
            @Override
            public int compare(Product o1, Product o2) {
                return 0;
            }

            @Override
            public Comparator<Product> reversed() {
                return Comparator.super.reversed();
            }

            @Override
            public Comparator<Product> thenComparing(Comparator<? super Product> other) {
                return Comparator.super.thenComparing(other);
            }

            @Override
            public <U> Comparator<Product> thenComparing(Function<? super Product, ? extends U> keyExtractor, Comparator<? super U> keyComparator) {
                return Comparator.super.thenComparing(keyExtractor, keyComparator);
            }

            @Override
            public <U extends Comparable<? super U>> Comparator<Product> thenComparing(Function<? super Product, ? extends U> keyExtractor) {
                return Comparator.super.thenComparing(keyExtractor);
            }

            @Override
            public Comparator<Product> thenComparingInt(ToIntFunction<? super Product> keyExtractor) {
                return Comparator.super.thenComparingInt(keyExtractor);
            }

            @Override
            public Comparator<Product> thenComparingLong(ToLongFunction<? super Product> keyExtractor) {
                return Comparator.super.thenComparingLong(keyExtractor);
            }

            @Override
            public Comparator<Product> thenComparingDouble(ToDoubleFunction<? super Product> keyExtractor) {
                return Comparator.super.thenComparingDouble(keyExtractor);
            }
        });

        treeMap2.put(pro1,100);
        treeMap2.put(pro2,200);
        treeMap2.put(pro3,300);

    }
}


class Product implements  Comparable<Product>{
    private int proId;
    private String proName;
    private  float proPrice;

    public Product(int proId,String proName,float proPrice){
        this.proId = proId;
        this.proName = proName;
        this.proPrice = proPrice;
    }
    public int getProId() {
        return proId;
    }

    public void setProId(int proId) {
        this.proId = proId;
    }

    public String getProName() {
        return proName;
    }

    public void setProName(String proName) {
        this.proName = proName;
    }

    public float getProPrice() {
        return proPrice;
    }

    public void setProPrice(float proPrice) {
        this.proPrice = proPrice;
    }

    //定义比较规则，实现先后顺序的比较
    //先比较name 字典顺序，若name相同 ， 比较价格，
    //若价格相同，比较id
    @Override
    public int compareTo(Product o) {
        if (this == o)
            return 0;
        if (this.proName.compareTo(o.proName)==0){ //"abc" "abc" ,继续比较 price
            if (this.proPrice == o.proPrice){ //继续比较id
                return this.proId - o.proId;
            }else {
                return (Float.valueOf(this.proPrice)).compareTo( o.proPrice);
            }
        }else
            return this.proName.compareTo(o.proName); //"abc" "efg"
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Product)) return false;
        Product product = (Product) o;
        return getProId() == product.getProId() && Float.compare(product.getProPrice(), getProPrice()) == 0 && Objects.equals(getProName(), product.getProName());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getProId(), getProName(), getProPrice());
    }

    @Override
    public String toString() {
        return "Product{" +
                "proId=" + proId +
                ", proName='" + proName + '\'' +
                ", proPrice=" + proPrice +
                '}';
    }
}